package com.mall.b2c.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.b2c.dao.DeliveryTemplateDao;
import com.mall.b2c.domain.delivery.DeliveryTemplateDomain;
import com.mall.b2c.service.DeliveryTemplateService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 快递单模版
 * @author lzx
 */
@Service("b2cDeliveryTemplateService")
@Transactional(readOnly=true)
public class DeliveryTemplateServiceImpl extends BaseServiceImpl<DeliveryTemplateDomain, Long> implements DeliveryTemplateService {
	
	@Resource
	public void setBaseDao(DeliveryTemplateDao deliveryTemplateDao) { 
		super.setBaseDao(deliveryTemplateDao); 
	}

	@Override
	public DeliveryTemplateDomain findDefault() {
		Finder finder = Finder.create("from DeliveryTemplateDomain d where d.isDefault = :isDefault");
		finder.setParam("isDefault", true);
		return (DeliveryTemplateDomain) finder.findUniqueByHQL();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(DeliveryTemplateDomain deliveryTemplate) {
		if(!this.isUnique("name", null, deliveryTemplate.getName())) {
			throw new NoticeException("该快递单模版名称已存在");
		}
		if(deliveryTemplate.getIsDefault() != null && deliveryTemplate.getIsDefault()){
			DeliveryTemplateDomain defaultDeliveryTemplate = findDefault();
			if(defaultDeliveryTemplate != null){
				defaultDeliveryTemplate.setIsDefault(false);
				this.update(defaultDeliveryTemplate);
			}
		}else{
			deliveryTemplate.setIsDefault(false);
		}
		this.persist(deliveryTemplate);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void edit(DeliveryTemplateDomain deliveryTemplate) {
		DeliveryTemplateDomain currentDeliveryTemplate = this.find(deliveryTemplate.getId());
		if(currentDeliveryTemplate == null) {
			throw new NoticeException("当前快递单模版不存在!");
		}
		if(!this.isUnique("name", currentDeliveryTemplate.getName(), deliveryTemplate.getName())) {
			throw new NoticeException("快递单模版已存在!");
		}
		if(deliveryTemplate.getIsDefault() != null && deliveryTemplate.getIsDefault()){
			DeliveryTemplateDomain defaultDeliveryTemplate = findDefault();
			if(defaultDeliveryTemplate != null){
				defaultDeliveryTemplate.setIsDefault(false);
				this.update(defaultDeliveryTemplate);
			}
		}else{
			deliveryTemplate.setIsDefault(false);
		}
		
		this.updateByUpdater(deliveryTemplate);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void switchDefault(Long newId) {
		DeliveryTemplateDomain newDeliveryTemplate = this.find(newId);
		if(newDeliveryTemplate == null){
			throw new NoticeException("该快递单模版不存在!");
		}
		if(!newDeliveryTemplate.getIsDefault()){
			DeliveryTemplateDomain defaultDeliveryTemplate = findDefault();
			if(defaultDeliveryTemplate != null){
				defaultDeliveryTemplate.setIsDefault(false);
				this.update(defaultDeliveryTemplate);
			}
			newDeliveryTemplate.setIsDefault(true);
		}else{
			newDeliveryTemplate.setIsDefault(false);
		}
		this.update(newDeliveryTemplate);
	}

}
